package server.utils;

import java.io.IOException;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;

import javax.servlet.annotation.WebListener;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionAttributeListener;
import javax.servlet.http.HttpSessionBindingEvent;

import org.springframework.util.StringUtils;

import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;


@WebListener
public class MySessionAttributeListener implements HttpSessionAttributeListener{

    private static AtomicInteger count=new AtomicInteger(0);
    private static AtomicInteger countU=new AtomicInteger(0);

    @Override
    public void attributeAdded(HttpSessionBindingEvent event) {
        int ss=count.incrementAndGet();
        HttpSession session=event.getSession();
        //String sessionId=(String) session.getAttribute("JPHPSESSID");
        String sessionId="123456";
        String attributeName = event.getName();
        Object attributeValue = event.getValue();
        System.out.println("Attribute add " + attributeName + " : " + attributeValue+",ss="+ss);
        String json=CooKieUtil.generateUUID();

        if(StringUtils.isEmpty(json)){
            return ;
        }
        String json_new;
        try {
            json_new = attributeAddOrUpdate(json,attributeName,attributeValue);
//            MemcachedUtil.setValue(sessionId, json_new);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    private String attributeAddOrUpdate(String json,String key,Object value)
            throws JsonParseException, JsonMappingException, IOException{
//        ObjectMapper mapper=JacksonMapUtil.getMapper();
//        @SuppressWarnings("unchecked")
//        Map<String,Object> userData = mapper.readValue(json, Map.class);
//        Boolean flag=String.class.isAssignableFrom(value.getClass());
//        if(!flag){
//            Map<String, Object> map = mapper.convertValue(value, Map.class);
//            userData.putAll(map);
//        }else{
//            userData.put(key, value);
//        }
        return null;
    }

    private String attributeDel(String json, String key)
            throws JsonParseException, JsonMappingException, IOException {
//        ObjectMapper mapper = JacksonMapUtil.getMapper();
//        @SuppressWarnings("unchecked")
//        Map<String, Object> userData = mapper.readValue(json, Map.class);
//        userData.remove(key);
        return null;
    }

    @Override
    public void attributeRemoved(HttpSessionBindingEvent event) {
        HttpSession session=event.getSession();
        //String sessionId=(String) session.getAttribute("JPHPSESSID");
        String sessionId="123456";
        String attributeName = event.getName();
        System.out.println("Attribute del : " + attributeName);
        String json=CooKieUtil.generateUUID();
        if(StringUtils.isEmpty(json)){
            return ;
        }
        String json_new;
        try {
            json_new = attributeDel(json,attributeName);
//            MemcachedUtil.setValue(sessionId, json_new);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    @Override
    public void attributeReplaced(HttpSessionBindingEvent event) {

        int ssu=countU.incrementAndGet();
        HttpSession session=event.getSession();
        //String sessionId=(String) session.getAttribute("JPHPSESSID");
        String sessionId="123456";
        String attributeName = event.getName();
        Object attributeValue = event.getValue();
        System.out.println("Attribute update " + attributeName + " : " + attributeValue+",ss="+ssu);
        String json=CooKieUtil.generateUUID();
        if(StringUtils.isEmpty(json)){
            return ;
        }
        String json_new;
        try {
            json_new = attributeAddOrUpdate(json,attributeName,attributeValue);
//            MemcachedUtil.setValue(sessionId, json_new);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

}
